<?
layout("/inc/win_layout.html",{title:sysTitle}){}
    ?>

<div class="page-content">
	<div class="row mbl">
			<div class="col-lg-12">
				<input type = "hidden" id = "columnId">
				<input type = "hidden" id = "classesEnHide">
				<input type = "hidden" id = "treeIdHide">
				<div class="input-group input-group-sm mbs">
					<table style="margin-top: 5px;">
						<tr id="openShowPage">
							
						</tr>
					</table>
				</div>
			</div>
		</div>
		<div>
			<p>已选择：(*输入框请输入排序值)</p>
			<p style="line-height: 200%" id="showText">
				
			</p>
			
		</div>
		<div id="showTable" style ="display: none;">
			<table class="table table-hover table-striped table-bordered table-advanced tablesorter mbn">
				<thead>
					<tr>
						<th style="width: 10%">请勾选</th>
						<th style="width: 7%">序号</th>
						<th style="width: 55%">标题</th>
						<th style="width: 20%">日期</th>
						<th style="width: 8%">封面</th>
					</tr>
				</thead>
				<tbody id="findUser">
				
				</tbody>
			</table>
			<div id="dataNull" class="panel-body"
					style="height: 90px; line-height: 50px;" align="center">
			</div>
			<div id="page2" style="text-align: center; margin-top: 20px;"></div>
		</div>
		
		<div id="showHtml" style ="display: none;">
			<textarea rows="24" class="form-control" cols="118" id = "htmlTextAll"></textarea>
		</div>
	</div>
	
<script type="text/javascript">
	function getQueryString(name) { 
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
	} 
	var arrays = new Array();
	var winName=getQueryString("winName");
	var typeId = window.parent.window[winName].getModuleType();
	var dynamicId = window.parent.window[winName].getDynamic();
	var contentId = window.parent.window[winName].getContent();
	$(function() {
		openShow(typeId)
		
	});
	
	function openShow(typeId){
		var content = "";
		if(typeId == 0){
			if(dynamicId == 1){
				$("#dynamic").show();
			}
			$("#showTable").show();
			content+='<td align="right" style="width: 100px;">新闻栏目：&nbsp;&nbsp;</td>';
			content+='<td ><input type="text" class="form-control" style="width: 80%;" id="columnName" value="根节点" disabled="true" /></td>';
			content+='<td >';
			content+='<button type="button" data-toggle="dropdown" class="btn btn-success dropdown-toggle" onclick="openColumn()">选择...</button>';
			content+='&nbsp;&nbsp;';
			content+='</td>';
			content+='<td align="right" style="width: 100px;">标题:&nbsp;&nbsp;</td>';
			content+='<td><input type="text" class="form-control" style="width: 80%;" id="realname" name="realname" /></td>';
			content+='<td align="right" style="width: 100px;">';
			content+='<button type="button" data-toggle="dropdown" class="btn btn-success dropdown-toggle" onclick=query("column")>查询</button>';
			content+='&nbsp;&nbsp;';
			content+='</td>';
			demo(1,"news",0)
		}else if(typeId == 1){
			if(dynamicId == 1){
				$("#dynamic").show();
			}
			$("#showTable").show();
			content+='<td  align="right" style="width: 100px;">资源库：&nbsp;&nbsp;</td>';
			content+='<td ><select id="relevanceClasses" class="form-control" onchange = checkClass()> </select></td>';
			content+='<td align="right" style="width: 100px;">标题&nbsp;&nbsp;</td>';
			content+='<td><input type="text" class="form-control" style="width: 80%;" id="realname" name="realname" /></td>';
			content+='<td align="right" style="width: 100px;">';
			content+='<button type="button" data-toggle="dropdown" class="btn btn-success dropdown-toggle" onclick="query()">查询</button>';
			content+='&nbsp;&nbsp;';
			content+='</td>';
			showClass();
		}else if(typeId == 2){
			$("#showHtml").show();
			content+='<td align="center"  style="width: 100%;">html内容：&nbsp;&nbsp;</td>';
		}
		$("#openShowPage").html(content);
	}
	
	function query(text){
		if(text == "column"){
			demo(1,"news",$("#treeIdHide").val());
		}else{
			demo(1,$("#classesEnHide").val(),0);
		}
	}
	function demo(curr,classesEn,treeId){
		$.ajax({
			type : "post",
			url : "classTypeShow.do",
			dataType : "json",
			data :{
				page : curr || 1,
				text : $("#realname").val(),
				classesEn: classesEn,
				treeId: treeId
			},
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(res){
				$("#classesEnHide").val(classesEn);
				$("#treeIdHide").val(treeId);
			    //alert(JSON.stringify(res));
				var str = "";
				$("#findUser").html("");
				if (res.count > 0) {
					$("#dataNull").html("");
				} else {
					$("#dataNull").html(
						"<span class='subtitle' style='font-size: 15px;'>数据为空!</span>");
				}
				var coverPath = "";
				for ( var i = 0; i < res.data.length; i++) {
					if(res.yesOrNo == "yes"){
						coverPath = res.data[i].cover;
					}
				//if(res.result[i].type == 1){
					str += "<tr id='tables'><td><input type='checkbox' id='"+res.data[i].id+"' name='checkall' nameEn ='"+classesEn+"' nameZh='"+res.data[i].title+"' cover='"+coverPath+"' value='"+res.data[i].id+"'/></td><td>"
							+ (i + 1)
							+ "</td><td> <label for="+res.data[i].id+">"
							+ res.data[i].title
							+ "</label></td>";
					if(res.data[i].date != undefined){
						str += "<td><label >"
								+ res.data[i].date
								+ "</label></td>";
					}else{
						str += "<td><label ></label></td>";
					}
					if(res.yesOrNo == "no"){
						str += 	"<td> <label ></label></td>;"
					}
					if(res.yesOrNo == "yes"){
						if(res.data[i].cover != ""){
							str += 	"<td> <label ><img src='"
								+ res.imagePath + res.data[i].cover
								+ "' style=' width: 25px; height: 25px; '/></label></td>;"
						}else{
							str += 	"<td> <label ></label></td>;"
						}
						
					}
					str += 	 "</tr>";
					//}
				}
				$("#findUser").append(str); 
				
				laypage({
					cont : document.getElementById('page2'), //容器。值支持id名、原生dom对象，jquery对象,
					pages : (Math.ceil(res.count/10)), //总页数
					skip : true, //是否开启跳页
					skin : 'molv', //加载内置皮肤，也可以直接赋值16进制颜色值，如:#c00
					curr: curr || 1,
					last: res.pageSize,
					groups : 6,//连续显示分页数
					jump : function(obj,first) { //触发分页后的回调
						if (!first) { //点击跳页触发函数自身，并传递当前页：obj.curr
							demo(obj.curr,classesEn,treeId);
							 curr = obj.curr;
						}
					}
				}); 
			}
		});
	
	}
	
	function checkClass(){
		demo(1,$("#relevanceClasses").val(),0);
		$("#columnId").val($("#relevanceClasses").val());
	}
	
	
	/*关联知识库*/
	function showClass(){
		$.ajax({
			type : "post",
			url : "selectClass.do",
			data : {
			},
			dataType : "json",
			success : function(data) {
				if(data.msg == "OK"){
					var content = "";
					demo(1,data.data[0].nameEn,0)
					$.each(data.data,function(k,v){
						content += '<option value="'+v.nameEn+'">'+v.nameZh+'</option>'
					})
					$("#relevanceClasses").html(content);
				}else{
					alert("请求失败");
				}
			},
			error : function(data) {
				alert("请求失败");
			}
		});
	}
	
	function openColumn(){
		parent.layer.open({
			type : 2,
			area : [ '560px', '600px' ],
			fix : false, //不固定
			scrollbar : false,
			shadeClose : true,
			//move : false,
			title : "选择关联新闻栏目",
			skin : 'layui-layer-rim', //加上边框
			btn : [ '保存 ', '关闭' ],
			content : [
					"cms-news-category-ztree.html?winName="+ window.name, 'no' ],
			yes : function(index, layer) {
				var iframeWin = parent.window[layer.find('iframe')[0]['name']];
				iframeWin.save();
			},
			cancel : function(index) {
			}
		});
	}

	function save(){
		var index = parent.layer.getFrameIndex(window.name);
		/* if(dynamicId == 1){
			window.parent.window[winName].$("#dataTypeId").val($("#columnId").val());
			parent.layer.close(index);
		}else{ */
			if(typeId != 2){
				var end = "";
				$("input[name='hideText']").each(function(j,item){    // 你要实现的业务逻辑    
				  	var t = item.value;
				  	var y = $("#"+t.substring(t.indexOf("∷")+1,t.indexOf("≡"))).val();
				  	end += t+"≡sort∷"+y+"###";
			    }); 
				window.parent.window[winName].$("#datasetText").val(end);
				parent.layer.msg('提交成功');
				parent.layer.close(index);
			}else{
			
				if($("#htmlTextAll").val()!=""){
					window.parent.window[winName].$("#htmlText").val($("#htmlTextAll").val());
					parent.layer.msg('提交成功');
					parent.layer.close(index);
				}else{
					alert("请书写HTML内容")
				}
			}
		/* } */
	}
	
	$("#tables").live("click", function () {
		/* $(this).find(":checkbox").attr("checked");
		alert($(this).find(":checkbox").attr("checked"));
		alert($(this).find(":checkbox").attr("id")); */
		/* if(dynamicId == 1){
			alert("动态数据不支持勾选");
			$(this).find(":checkbox").attr("checked", false);
			return; 
		} */
		if($(this).find(":checkbox").attr("checked") == "checked"){
			var content = "";
			var s;
			if(contentId ==0){
				if(typeId == 0){
					s = "id∷"+$(this).find(":checkbox").attr("id")+"≡title∷"+$(this).find(":checkbox").attr("nameZh");
				}else{
					s = "id∷"+$(this).find(":checkbox").attr("id")+"≡title∷"+$(this).find(":checkbox").attr("nameZh")+"≡classEn∷"+$(this).find(":checkbox").attr("nameEn");
				}
				/* content+='<span id="'+$(this).find(":checkbox").attr("id")+'i">';
				content+='<input id="'+$(this).find(":checkbox").attr("id")+'" style="width:25px;margin-left: 5px;" type="text" value="0" />';
				content+='<input name="hideText" type="hidden" value="'+s+'" />';
				content+='<span class="label label-sm label-success" style="height: 50px;margin-left: 5px;">'+$(this).find(":checkbox").attr("nameZh")+'</span>';
				content+='<button type="button" data-toggle="dropdown" class="btn btn-success dropdown-toggle" style="background-color: red;border-color: red;margin-left: 5px;" onclick=deleteSpan("'+$(this).find(":checkbox").attr("id")+'i")>删除</button>';
				content+='</span>'; */
			}else{
				if(typeId == 0){
					s = "id∷"+$(this).find(":checkbox").attr("id")+"≡title∷"+$(this).find(":checkbox").attr("nameZh")+"≡cover∷"+$(this).find(":checkbox").attr("cover");
				}else{
					s = "id∷"+$(this).find(":checkbox").attr("id")+"≡title∷"+$(this).find(":checkbox").attr("nameZh")+"≡cover∷"+$(this).find(":checkbox").attr("cover")+"≡classEn∷"+$(this).find(":checkbox").attr("nameEn");
				}
			}
				content+='<span id="'+$(this).find(":checkbox").attr("id")+'i">';
				content+='<input id="'+$(this).find(":checkbox").attr("id")+'" style="width:25px;margin-left: 5px;" type="text" value="0" />';
				content+='<input name="hideText" type="hidden" value="'+s+'" />';
				content+='<span class="label label-sm label-success" style="height: 50px;margin-left: 5px;">'+$(this).find(":checkbox").attr("nameZh")+'</span>';
				content+='<button type="button" data-toggle="dropdown" class="btn btn-success dropdown-toggle" style="background-color: red;border-color: red;margin-left: 5px;" onclick=deleteSpan("'+$(this).find(":checkbox").attr("id")+'i")>删除</button>';
				content+='</span>';
			
			$("#showText").append(content);
			
			/* arrays.push(s) */
		}
		
	     /* if ($(this).hasClass("bgRed")) {
	         $(this).removeClass("bgRed").find(":checkbox").attr("checked", false);
	     } else {
	     	 $("input[name='checkall']").attr("checked", false);
	     	 $("input[name='checkall']").removeClass("bgRed");
	     	/*  $("tr").removeClass("bgRed").find("tr input").attr("checked", false); 
             $(this).addClass("bgRed").find(":checkbox").attr("checked", true);
         } */
	 });
	 
	 function deleteSpan(ids){
	 	$("#"+ids).remove();
	 }
</script>
